import { BaseService } from '@app/db/base/base.service';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { PlatformData } from '../entities/PlatformData';

@Injectable()
export class PlatformDataService extends BaseService<PlatformData>{
    constructor(
        @InjectRepository(PlatformData, 'common') protected repository:Repository<PlatformData>
    ) {
        super()
        this.primaryKey = 'id'
    }

    async queryData(feilds?:Array<keyof PlatformData>) {
        const data = await this.find(null, feilds, 0, 1)
        return data.length > 0 ? data[0] : null
    }

    async updateData(dto:{[key in keyof PlatformData]? : PlatformData[key]}) {
        const data = await this.find(null, ['id'], 0, 1)
        if(data.length == 0) {
            return this.insert(dto)
        }else {
            return this.update({id : data[0].id}, dto)
        }
    }
}
